<?php
include_once("../tools/DBInterface.class.php");
include_once("Player.class.php");
include_once("../Delegation/Delegation.class.php");
include_once("../lib/Smarty/Smarty.class.php");

$smarty = new Smarty();
$player = new Player();
$dbinterface = new DBInterface();

switch($_GET['action'])
{
    case 1://When the section player is called from the menu
        $params = array();
        $params['pages'] = $player->getNumPages();
        $params['pg_at'] = 1;
        $smarty->assign("tourney",$dbinterface->exec_select(array('*'),'tourney'));
        $smarty->assign("params",$params);
        $smarty->assign("players",$player->getList('','',10,0));
        $smarty->display("Player/players_main.tpl");
    break;

    case 2://When the user press the tab players
        $params = array();
        $params['pages'] = $player->getNumPages();
        $params['pg_at'] = 1;
        $smarty->assign("tourney",$dbinterface->exec_select(array('*'),'tourney'));
        $smarty->assign("params",$params);
        $smarty->assign("players",$player->getList('','',10,0));
        $smarty->display("Player/players_main_cont.tpl");
    break;

    case 3://When the autocompleter is requested
        $smarty->assign("elements",$player->getNames($_POST['name'], $_POST['s_type']));
        $smarty->display("autocomplete.tpl");
    break;

    case 4://When the user has pressed the Search button
        $params = $_POST;
        $params['pages'] = $player->getNumPages($_POST['name'], $_POST['s_type']);
        $params['pg_at'] = 1;
        $smarty->assign("params",$params);
        $smarty->assign("players",$player->getList($_POST['name'], $_POST['s_type'], 10, 0));
        $smarty->display("Player/players_list.tpl");
    break;

    case 5://When the user press the tab new player
        $smarty->assign("tourney",$dbinterface->exec_select(array('*'),'tourney'));
        $smarty->assign("delegations",$dbinterface->exec_select(array('del_id','del_name'),'delegation','del_id<>1'));
        $smarty->display("Player/players_new.tpl");
    break;

    case 6://To create a new player
        if($player->create($_POST))
        {
            echo '<script type="text/javascript">
                    alert("Jugador creado exitosamente");
                </script>';
        }
        else
        {
            echo '<script type="text/javascript">
                    alert("Ha ocurrido un error, intentalo de nuevo");
                </script>';
        }
        $smarty->assign("tourney",1);
        $smarty->assign("delegations",$dbinterface->exec_select(array('del_id','del_name'),'delegation','del_id<>1'));
        $smarty->display("Player/players_new.tpl");
    break;

    case 7://To visualize the info of a player
        $smarty->assign("player",$player->getData($_GET['id']));
        $smarty->assign("delegations",$dbinterface->exec_select(array('del_id','del_name'),'delegation','del_id<>1'));
        $smarty->display("Player/players_info.tpl");
    break;

    case 8://To modify a player
        if($_POST['value']=='' || str_replace(' ','',$_POST['value'])=='')
        {
            echo '<script type="text/javascript">
                    alert("Escribe algún valor");
                </script>';
            echo $_POST['oldtext'];
            break;
        }
        if($_POST['name']=='pla_first_name' || $_POST['name']=='pla_last_name' || $_POST['name']=='pla_place_birth')
        {
            if(is_numeric($_POST['value']))
            {
                echo '<script type="text/javascript">
                        alert("Este campo no debe ser un número");
                    </script>';
                echo $_POST['oldtext'];
                break;
            }
            $name = str_split(str_replace(' ','',$_POST['value']));
            sort($name);
            if(is_numeric($name[0]))
            {
                echo '<script type="text/javascript">
                        alert("Este campo no debe contener números");
                    </script>';
                echo $_POST['oldtext'];
                break;
            }
        }
        if($_POST['name']=='pla_del_id')
        {
            $delegation = new Delegation();
            $pla = $player->getData($_POST['id']);
            if($delegation->isFull($_POST['value'],$pla['pla_gender']))
            {
                echo '<script type="text/javascript">
                        alert("Esta delegación no tiene cupos disponibles");
                    </script>';
                echo $_POST['oldtext'];
                break;
            }
        }
        if($_POST['name']=='pla_document')
        {
            if($player->exist($_POST['value']))
            {
                echo '<script type="text/javascript">
                        alert("Ya existe un jugador con este documento");
                    </script>';
                echo $_POST['oldtext'];
                break;
            }
        }
        if($player->modify($_POST['name'],$_POST['value'],$_POST['id'])===false)
        {
            echo '<script type="text/javascript">
                    alert("Formato incorrecto");
                </script>';
            echo $_POST['oldtext'];
        }
        else
        {
            if($_POST['name']=='pla_first_name' || $_POST['name']=='pla_last_name')
            {
                $name = $player->getData($_POST['id']);
                echo '<script type="text/javascript">
                        document.getElementById(\'pla_title\').innerHTML = "Jugador '.$name['pla_first_name'].' '.$name['pla_last_name'].'";
                    </script>';
            }
            echo '<script type="text/javascript">
                      updater(\''.$_POST['id'].'\',\'Player/WP_Player.php?action=9&id='.$_POST['id'].'\');
                  </script>';
            if($_POST['name']=='pla_gender')
            {
                if($_POST['value']=='m')
                    $value = "Masculino";
                else
                    $value = "Femenino";
            }
            if($_POST['name']=='pla_del_id')
            {
                $delegation = new Delegation();
                $info = $delegation->getData($_POST['value']);
                $value = $info['del_name'];
            }
            else
                $value = $_POST['value'];
            echo $value;
        }
    break;

    case 9://To actualize the row of the table where its the player
        $smarty->assign("pla",$player->getData($_GET['id']));
        $smarty->display("Player/players_info_row.tpl");
    break;

    case 10://To delete a player
        $player->remove($_GET['id']);
        echo '<script type="text/javascript">
                updater(\'list\',\'Player/WP_Player.php?action=4\');
            </script>';
    break;

    case 11://To automplete the place of birth
        $smarty->assign("elements",$player->getPlaces($_POST['pla_place_birth']));
        $smarty->display("autocomplete.tpl");
    break;

    case 12://To navigate into pages
        $pg_at = $_GET['pg_at'];
        if($_GET['pgn']=='next')
            $_GET['pg_at'] = $pg_at+1;
        elseif($_GET['pgn']=='prev')
        {
            $_GET['pg_at'] = $pg_at-1;
            $pg_at -= 2;
        }
        elseif($_GET['pgn']=='first')
        {
            $_GET['pg_at'] = 1;
            $pg_at = 0;
        }
        else
        {
            $_GET['pg_at'] = $_GET['pages'];
            $pg_at = $_GET['pages']-1;
        }
        $smarty->assign("params",$_GET);
        $smarty->assign("players",$player->getList($_GET['name'], $_GET['s_type'], 10, ($pg_at*10)));
        $smarty->display("Player/players_list_pg.tpl");
    break;

    case 13://Validate if a player exists
        if($player->exist($_GET['doc']))
            echo 'true';
        else
            echo 'false';
    break;

    case 14://Validate if a player has a event's game
        if($player->hasEvent($_GET['id']))
            echo 'true';
        else
            echo 'false';
    break;

    case 15://Validate if a player has records
        if($player->hasRecord($_GET['id']))
            echo 'true';
        else
            echo 'false';
    break;
}
?>